<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Tour_buy extends CI_Controller{
      protected $_templates;
      function __construct() {
		parent::__construct();
		$this->pre_message = "";     
		$this->permit_lib->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));
		$this->lang->load('tour_buy');
		$this->load->library('string_library');
		$this->load->model('tour_buy_model','tour_buy');
		$this->load->model('tour_good_model','tourgood');
		$this->load->model('city_model','city');
      }   
	  function index(){
		  $this->listtour_buy();
	  }
	  function tour_price($tour_good_price_id=0){
		  $rs =  $this->tourgood->get_object($tour_good_price_id);
		 echo  $tour_price = number_format($rs->price,'0','','.').' '.$rs->currency_name;
	  }
      function listtour_buy(){
         
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('full_name'=>'Tên khách hàng'),'tour_buy/listtour_buy/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
			
            /*End  search*/
            $data['title'] = 'Danh sách đặt tour';
            $data['add'] = 'tour_buy/add';
            $config['base_url'] = base_url().'tour_buy/listtour_buy/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->tour_buy->get_num_data($arr_search);
           	$config['total_rows']   =  $data['num'];
         	$config['per_page']  =   '20';
           	$config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['list'] =   $this->tour_buy->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            $data['pagination']    = $this->pagination->create_links();       
            $this->_templates['page'] = 'tour_buy/listtour_buy';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
    
      function add(){                         
			$data['title'] = 'Thêm mới đặt tour';
			$data['option_pid'] = $this->city->optionCity();
			$data['tougoodItems'] = $this->tourgood->getAllData();
			$this->form_validation->set_rules('full_name',lang('bk.full_name'),'trim|required');
			$this->form_validation->set_rules('tell',lang('bk.tell'),'trim|required');
			$this->form_validation->set_rules('email','Email','trim|required');
			$this->form_validation->set_rules('address',lang('bk.address'),'trim|required');
			$this->form_validation->set_rules('city_id',lang('bk.city'),'trim|required');
			$this->form_validation->set_rules('tour_code',lang('bk.code'),'trim|required');
			$this->form_validation->set_rules('tour_price',lang('bk.price_tour'),'trim|required');
			$this->form_validation->set_rules('people_grown_up',lang('bk.people_old'),'trim|required');
			// ghi lai file cau hinh duong dan cua tinymce
		
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $data = $this->build_data($_POST,1);
				 if($this->tour_buy->saveData(0,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('tour_buy/listtour_buy');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
					 
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'tour_buy/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){                         
            $data['title'] = 'Sửa đặt tour';
            $data['query'] = $this->tour_buy->get_object($id);
           	$data['option_pid'] = $this->city->optionCity($data['query']->city_id);
			$data['tougoodItems'] = $this->tourgood->getAllData($data['query']->tour_good_price_id);
			$this->form_validation->set_rules('full_name',lang('bk.full_name'),'trim|required');
			$this->form_validation->set_rules('tell',lang('bk.tell'),'trim|required');
			$this->form_validation->set_rules('email','Email','trim|required');
			$this->form_validation->set_rules('address',lang('bk.address'),'trim|required');
			$this->form_validation->set_rules('city_id',lang('bk.city'),'trim|required');
			$this->form_validation->set_rules('tour_code',lang('bk.code'),'trim|required');
			$this->form_validation->set_rules('tour_price',lang('bk.price_tour'),'trim|required');
			$this->form_validation->set_rules('people_grown_up',lang('bk.people_old'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				$data = $this->build_data($_POST,1);
				 if($this->tour_buy->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('tour_buy/listtour_buy');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				}
		 }
          $data['message'] = $this->pre_message;
		  $data['tour_good_id'] = $data['query']->tour_good_price_id;
          $this->_templates['page'] = 'tour_buy/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->tour_buy->delete($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('tour_buy/listtour_buy/'.$page);
    }
    function dels(){
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->tour_buy->delete($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('tour_buy/listtour_buy/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
				$dataUpdate["full_name"]=trim($data["full_name"]);
				$date = DateTime::createFromFormat('m/d/Y', $data["dt_start"]);
				if(is_object($date)) {
					$date_start = $date->format('Y-m-d');
				}
				$dataUpdate["dt_start"]=$date_start;
				$dataUpdate["phone"]=trim($data["tell"]);
				$dataUpdate["email"]=$data["email"];
				$dataUpdate["address"]=trim($data["address"]);
				$dataUpdate["city_id"]=$data["city_id"];
				$dataUpdate["tour_good_price_id"]=trim($data["tour_code"]);
				$dataUpdate["num_grown_up"]=trim($data["people_grown_up"]);
				$dataUpdate["num_child"]=trim($data["num_child"]);
				$dataUpdate["nb_customer"]=$data["people_grown_up"] + $data["num_child"] ;
				$dataUpdate["orther_request"]=trim($data["note"]);
				$price_tour = str_replace('.','',$data["tour_price"]);
				$price = str_replace('VNĐ','',$price_tour);
				$dataUpdate["total_price"]=$data["people_grown_up"] * $price + $data["num_child"]* $price *0.5 ;
				$dataUpdate["tour_price"]=$price;
				if($flag==1)
               		$dataUpdate["dt_create"] = date('Y-m-d H:s:i');
                if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}

  }
?>
